% summary stats
tic
cd(folder_data)
load simulated_data 

% -------------------- %
% Skipper Distribution %
% -------------------- %

% disp('  total Number of ranked schools:')
tab_length = NaN(1,P);
for pp = 1:P
    % Tabulate the total number of ranked school
    clear tab
    tab_length(pp) = mean(sim_data_Mis(1+I*J*M*(pp-1):I*J*M*pp,13));  
end


tab_skips = NaN(J,P+1);
tab_skips(:,1) = 12-(1:J);
for pp = 1:P
    % Tabulate the number of ranked school
    clear tab
    tab = tabulate(sim_data_Mis(1+I*J*M*(pp-1):I*J*M*pp,13));
    %     tab_skips(:,1+pp) = tab(:,3);
    
    for jj = 1:J
        for kk = 1:size(tab,1)
            if tab(kk,1) == jj
                tab_skips(jj,1+pp) = tab(kk,3);
            end
        end
    end
end

% Number of ranked schools: T=1 students
tab_skips_AA = NaN(J,P+1);
tab_skips_AA(:,1) = 12-(1:J);
for pp = 1:P
    % Tabulate the number of ranked school
    clear tab
    tab = tabulate(sim_data_Mis(1+I*J*M*(pp-1):I*J*M*pp,13).*sim_data_Mis(1+I*J*M*(pp-1):I*J*M*pp,23));
    %     tab_skips(:,1+pp) = tab(:,3);
    
    for jj = 1:J
        for kk = 1:size(tab,1)
            if tab(kk,1) == jj
                tab_skips_AA(jj,1+pp) = tab(kk,2); % frequences
            end
        end
    end
end
tab_skips_AA(:,3:P+1) = tab_skips_AA(:,3:P+1)/tab_skips_AA(J,2)*100;

% Number of ranked schools: T=0 students
tab_skips_nAA = NaN(J,P+1);
tab_skips_nAA(:,1) = 12-(1:J);
for pp = 1:P
    % Tabulate the number of ranked school
    clear tab
    tab = tabulate(sim_data_Mis(1+I*J*M*(pp-1):I*J*M*pp,13).*(1-sim_data_Mis(1+I*J*M*(pp-1):I*J*M*pp,23)));
    %     tab_skips(:,1+pp) = tab(:,3);
    
    for jj = 1:J
        for kk = 1:size(tab,1)
            if tab(kk,1) == jj
                tab_skips_nAA(jj,1+pp) = tab(kk,2); % frequences
            end
        end
    end
end
tab_skips_nAA(:,3:P+1) = tab_skips_nAA(:,3:P+1)/tab_skips_nAA(J,2)*100;


% -------------------- %
% Ranking Top K        %
% -------------------- %

% Average % students rank top k schools
tab_topk = NaN(2,P+1);
tab_topk(:,1) = 0:1;
for pp = 1:P
    clear tab
    tab = tabulate(sim_data_Mis(1+I*J*M*(pp-1):I*J*M*pp,20));
%     tab_topk(:,pp) = tab(:,3);
    for jj = 1:2
        for kk = 1:size(tab,1)
            if tab(kk,1) == jj-1
                tab_topk(jj,1+pp) = tab(kk,3);
            end
        end
    end
end

% T = 1 students
tab_topk_AA = NaN(2,P+1);
tab_topk_AA(:,1) = 0:1;
for pp = 1:P
    clear tab
    tab = tabulate(sim_data_Mis(1+I*J*M*(pp-1):I*J*M*pp,20).*sim_data_Mis(1+I*J*M*(pp-1):I*J*M*pp,23));
%     tab_topk(:,pp) = tab(:,3);
    for jj = 1:2
        for kk = 1:size(tab,1)
            if tab(kk,1) == jj-1
                tab_topk_AA(jj,1+pp) = tab(kk,2);
            end
        end
    end
end
tab_topk_AA(2,3:P+1) = tab_topk_AA(2,3:P+1)/tab_topk_AA(2,2)*100;

% T = 0 students
tab_topk_nAA = NaN(2,P+1);
tab_topk_nAA(:,1) = 0:1;
for pp = 1:P
    clear tab
    tab = tabulate(sim_data_Mis(1+I*J*M*(pp-1):I*J*M*pp,20).*(1-sim_data_Mis(1+I*J*M*(pp-1):I*J*M*pp,23)));
%     tab_topk(:,pp) = tab(:,3);
    for jj = 1:2
        for kk = 1:size(tab,1)
            if tab(kk,1) == jj-1
                tab_topk_nAA(jj,1+pp) = tab(kk,2);
            end
        end
    end
end
tab_topk_nAA(2,3:P+1) = tab_topk_nAA(2,3:P+1)/tab_topk_nAA(2,2)*100;

% -------------------- %
% No blocking          %
% -------------------- %

% Assigned to the most preferred among feasible schools
tab_stable = NaN(2,P+1);
tab_stable(:,1) = 0:1;
for pp = 1:P
    clear tab
    tab = tabulate(sim_data_Mis(1+I*J*M*(pp-1):I*J*M*pp,17));
%     tab_stable(:,pp) = tab(:,3);
    for jj = 1:2
        for kk = 1:size(tab,1)
            if tab(kk,1) == jj-1
                tab_stable(jj,1+pp) = tab(kk,3);
            end
        end
    end
end

% T = 1 students
tab_stable_AA = NaN(2,P+1);
tab_stable_AA(:,1) = 0:1;
for pp = 1:P
    clear tab
    tab = tabulate(sim_data_Mis(1+I*J*M*(pp-1):I*J*M*pp,17).*sim_data_Mis(1+I*J*M*(pp-1):I*J*M*pp,23));
%     tab_stable(:,pp) = tab(:,3);
    for jj = 1:2
        for kk = 1:size(tab,1)
            if tab(kk,1) == jj-1
                tab_stable_AA(jj,1+pp) = tab(kk,2);
            end
        end
    end
end
tab_stable_AA(2,3:P+1) = tab_stable_AA(2,3:P+1)/tab_stable_AA(2,2)*100;

% T = 0 students
tab_stable_nAA = NaN(2,P+1);
tab_stable_nAA(:,1) = 0:1;
for pp = 1:P
    clear tab
    tab = tabulate(sim_data_Mis(1+I*J*M*(pp-1):I*J*M*pp,17).*(1-sim_data_Mis(1+I*J*M*(pp-1):I*J*M*pp,23)));
%     tab_stable(:,pp) = tab(:,3);
    for jj = 1:2
        for kk = 1:size(tab,1)
            if tab(kk,1) == jj-1
                tab_stable_nAA(jj,1+pp) = tab(kk,2);
            end
        end
    end
end
tab_stable_nAA(2,3:P+1) = tab_stable_nAA(2,3:P+1)/tab_stable_nAA(2,2)*100;

%%%%%%%
disp('Distributions of Skippers')
disp(tab_skips)

disp('Fractions skipping')
disp(tab_topk(1,:))

disp('Average length of ROLs')
disp(tab_length)

disp('Fractions WTT')
disp(tab_topk(2,:))

disp('Fractions of students assigned most prefarred feasible school')
disp(tab_stable(2,:))

tab2 = table('Size',[3,3],'VariableTypes',{'double','double','double'},...
    'VariableNames',{'Truth-Telling','Payoff Irrelevant Mistakes',...
    'Payoff Relevant Mistakes'},...
    'RowNames',{'Average length of submitted ROLs',...
    'WTT: Weak Truth-Telling (%)',...
    'Matched with favorite feasible college (%)'});
for ii = 1:3
    tab2(1,ii) = table(tab_length(1,ii));
    tab2(2,ii) = table(tab_topk(2,ii+1));
    tab2(3,ii) = table(tab_stable(2,ii+1));
end

cd(folder_tables)
writetable(tab2,'tab_2.csv','WriteRowNames',true)
disp('Table 2 (summary stats) in the paper: tab_2.csv; saved')
disp(tab2)
toc